package com.aqie.easy.stack;

import java.util.Stack;

/**
 * 1047 删除字符串中所有相邻重复项
 */
public class RemoveDuplicates {
    public static String removeDuplicates(String S) {
        Stack<Character> stack = new Stack<>();
        stack.push(S.charAt(0));
        for (int i = 1; i < S.length(); i++){
            if (stack.isEmpty() || stack.peek() != S.charAt(i)){
                stack.push(S.charAt(i));
            }else {
                stack.pop();
            }
        }

        StringBuilder sb = new StringBuilder();
        for (Character character : stack){
            sb.append(character);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "abbaca";
        System.out.print(removeDuplicates(str));
    }
}
